function minWindow(s, t) {
    const need = {};
    for (const c of t) {
        need[c] = (need[c] || 0) + 1;
    }
    const window = {};
    let left = 0, right = 0;
    let valid = 0;
    let start = 0, minLen = Infinity;
    const needSize = Object.keys(need).length;

    while (right < s.length) {
        const c = s[right];
        right++;
        if (need[c]) {
            window[c] = (window[c] || 0) + 1;
            if (window[c] === need[c]) {
                valid++;
            }
        }

        while (valid === needSize) {
            if (right - left < minLen) {
                minLen = right - left;
                start = left;
            }
            const d = s[left];
            left++;
            if (need[d]) {
                if (window[d] === need[d]) {
                    valid--;
                }
                window[d]--;
            }
        }
    }

    return minLen === Infinity ? "" : s.substring(start, start + minLen);
}